package com.wondertek.poms.core.service.impl;

import com.wondertek.poms.common.service.impl.ServiceImpl;
import com.wondertek.poms.core.service.ISpecialConfigGameService;
import com.wondertek.poms.dao.po.SpecialConfigGame;
import com.wondertek.poms.dao.repo.ISpecialConfigGameDao;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.Predicate;
import javax.transaction.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author evan0213
 * @description
 */
@Service
public class SpecialConfigGameServiceImpl extends ServiceImpl<SpecialConfigGame, Long> implements ISpecialConfigGameService {

	@Autowired
	private ISpecialConfigGameDao specialConfigGameDao;

	@Override
	public SpecialConfigGame deleteLogic(Long id) {
		return null;
	}

	@Override
	public SpecialConfigGame merge(SpecialConfigGame model) {
		return null;
	}

	public Page<SpecialConfigGame> findAll(SpecialConfigGame entity, int page, int rows, String order, String column) {
		Pageable pageable = getPageable(page - 1, rows, column, order);
		Specification<SpecialConfigGame> specification = getWhereClause(entity);
		return specialConfigGameDao.findAll(specification, pageable);

	}

	private Specification<SpecialConfigGame> getWhereClause(SpecialConfigGame specialConfigGame) {
		return (Specification<SpecialConfigGame>) (root, query, cb) -> {
			List<Predicate> predicate = new ArrayList<>();
			if (StringUtils.isNotBlank(specialConfigGame.getName())) {
				predicate.add(cb.like(root.get("name"), "%" + specialConfigGame.getName() + "%"));
			}
			if (StringUtils.isNotBlank(specialConfigGame.getDetails())) {
				predicate.add(cb.like(root.get("details"), "%" + specialConfigGame.getDetails() + "%"));
			}
			if (StringUtils.isNotBlank(specialConfigGame.getKey())) {
				predicate.add(cb.equal(root.get("key"), specialConfigGame.getKey()));
			}
			Predicate[] pre = new Predicate[predicate.size()];
			return query.where(predicate.toArray(pre)).getRestriction();
		};
	}

	@Override
	public SpecialConfigGame findByKey(String key) {
		return specialConfigGameDao.findByKey(key);
	}

	@Override
	public SpecialConfigGame findByName(String name) {
		return specialConfigGameDao.findByName(name);
	}

	@Override
	public int deleteByKey(String key) {
		return specialConfigGameDao.deleteByKey(key);
	}

	@Override
	public List<SpecialConfigGame> findAllByKeys(List<String> keys) {
		return specialConfigGameDao.findAllByKeys(keys);
	}

	@Transactional
	@Override
	public int deleteByKeys(List<String> asList) {
		return specialConfigGameDao.deleteByKeys(asList);
	}

	@Transactional
	@Override
	public int updateStatus(Integer status, String key) {
		return specialConfigGameDao.updateStatusByKey(status, key);
	}

	@Override
	public SpecialConfigGame findByKeyAndName(String key, String name) {
		return specialConfigGameDao.findByKeyAndName(key, name);
	}
}
